package com.sylar.lelepub;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;

import com.sylar.lelepub.adapter.CityAdapter;
import com.sylar.lelepub.domain.CityInfo;
import com.sylar.lelepub.tools.PullParseCityXml;
import com.sylar.lelepub.widget.LetterListView;
import com.sylar.lelepub.widget.LetterListView.OnTouchingLetterChangedListener;

public class SearchCityActivity extends Activity{
	private TextView overlay;
	private LetterListView city_letterview;
	private ListView city_list;
	private ArrayList<CityInfo> cityList;
	private HashMap<String, Integer> alphaIndexer;
	private OverlayThread overlayThread;
	private Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.layout_search_city);

		View search_bar = getLayoutInflater().inflate(R.layout.layout_search_bar, null);
		
		overlay = (TextView) findViewById(R.id.tv_select_search);
		overlay.setVisibility(View.GONE);
		
		city_letterview = (LetterListView) findViewById(R.id.city_letterview);
		city_letterview.setOnTouchingLetterChangedListener(new LetterListViewListener());
		city_list = (ListView) findViewById(R.id.city_list);
		city_list.addHeaderView(search_bar, null, false);
		
		TextView tv_title_name = (TextView) findViewById(R.id.tv_title_name);
		tv_title_name.setText("入住城市");
		
		View btn_title_right = findViewById(R.id.btn_title_right);
		btn_title_right.setVisibility(View.GONE);
		
//		View btn_title_left = findViewById(R.id.btn_title_left);
//		btn_title_left.setVisibility(View.VISIBLE);
//		btn_title_left.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View arg0) {
//				finish();
//			}
//		});
		
		overlayThread = new OverlayThread();
		
		handler = new Handler();
		
		initData();

		setData();

	}

	private class LetterListViewListener implements OnTouchingLetterChangedListener {

		@Override
		public void onTouchingLetterChanged(final String s) {
			if (alphaIndexer.get(s) != null) {
				int position = alphaIndexer.get(s);
				city_list.setSelection(position);
				
				overlay.setText(s);
				
				overlay.setVisibility(View.VISIBLE);

				handler.removeCallbacks(overlayThread);
				
				handler.postDelayed(overlayThread, 1000);

			}
		}

	}

	private class OverlayThread implements Runnable {

		@Override
		public void run() {
			overlay.setVisibility(View.GONE);
		}

	}

	final void initData() {
		AssetManager am = getAssets();
		InputStream is = null;
		try {
			is = am.open("citylist.xml");

		} catch (Exception e) {
			e.printStackTrace();
		}
		PullParseCityXml parseXml = new PullParseCityXml(is);

		initList(parseXml);
	}


	final void initList(PullParseCityXml parseXml) {

		cityList = new ArrayList<CityInfo>();

		List<CityInfo> cityTempList = new ArrayList<CityInfo>();

		ArrayList<String> root = parseXml.root;
		HashMap<String, ArrayList<CityInfo>>  cityMap = parseXml.map;

		for (int i = 0; i < root.size(); i++) {
			String tempKey = root.get(i);
			cityTempList = cityMap.get(tempKey);
			for (int j = 0; j < cityTempList.size(); j++) {
				cityList.add(cityTempList.get(j));
			}
		}

	}

	CityAdapter adapter;

	public void setData() {
		if (alphaIndexer == null) {
			alphaIndexer = new HashMap<String, Integer>();
		}
		for (int i = 0; i < cityList.size(); i++) {
			CityInfo cityInfo = cityList.get(i);
			String currentAlpha = cityInfo.shortName.substring(0, 1).toUpperCase();
			CityInfo info = (i - 1) >= 0 ? cityList.get(i - 1) : null;
			String previewStr = "";
			if (info != null) {
				previewStr = info.shortName.substring(0, 1).toUpperCase();
			}
			
			
			if (!previewStr.equals(currentAlpha)) {
				alphaIndexer.put(currentAlpha, i);
			}
		}

		adapter = new CityAdapter(getApplicationContext(), cityList);
		city_list.setAdapter(adapter);
	}

}
